#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string FindMax(const string &brand)
{
    if(brand.find("joker JOKER") != string::npos)
        return "joker JOKER";
    
    int dash = brand.find('-');
    //分开两手牌
    string car1 = brand.substr(0,dash);
    string car2 = brand.substr(dash+1);

    //获取两首牌的张数，判断是否是相同类型
    int car1_cnt = count(car1.begin(),car1.end(),' ') + 1;
    int car2_cnt = count(car2.begin(),car2.end(),' ') + 1;

    //获取两手牌的各自第一张牌
    string car1_first = car1.substr(0,car1.find(' '));
    string car2_first = car2.substr(0,car2.find(' '));

    if(car1_cnt == car2_cnt) //两手牌的类型相同
    {
        string str = "345678910JQKA2jokerJOKER";
        if(str.find(car1_first) > str.find(car2_first))
            return car1;
        return car2;
    }

    if(car1_cnt == 4) //说明是炸弹
        return car1;
    else if(car2_cnt == 4) 
        return car2;
    
    return "ERROR";
}

int main()
{
    string brand,res;
    while(getline(cin,brand))
    {
        res = FindMax(brand);
        cout << res << endl;
    }
}
